//
//  MSTagSelectorView.m
//  JYG_Agent
//
//  Created by apple on 15/10/21.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "MSTagSelectorView.h"

@implementation MSTagSelectorView

@synthesize selected;

-(id)init
{
    self=[super init];
    if (self)
    {
        [self initUI];
    }
    return self;
}

-(void)initUI
{
    int x=15,y=10;
    CGRect rect=CGRectMake(x, y, APPFRAME.size.width-2*x, 20);
    UILabel* titleLabel=[[UILabel alloc] initWithFrame:rect];
    titleLabel.tag=101;
    titleLabel.font=[UIFont fontWithName:@"STHeitiSC-Light" size:16];
    titleLabel.textColor = kDarkGrayColor;
    [self addSubview:titleLabel];
}

-(void)setTitle:(NSString *)title
{
    _title=title;
    UILabel* titleLabel=(UILabel*)[self viewWithTag:101];
    [titleLabel setText:title];
}

-(void)setSelected:(NSString *)value
{
    selected=value;
    if (value!=nil)
    {
        NSArray* selectedItems=[value componentsSeparatedByString:@"/"];
        for (NSString* item in selectedItems)
        {
            for (int i=0; i<self.tags.count; i++)
            {
                UIButton* btn=(UIButton*)[self viewWithTag:(200+i)];
                if ([btn.titleLabel.text isEqualToString:item])
                {
                    btn.selected=YES;
                    if (self.isSingle)
                    {
                        self.curBtn=btn;
                    }
                    btn.layer.borderColor=kBlueColor.CGColor;
                }
            }
            
        }
    }
    else
    {
        [self cleanSelected];
    }
}

-(void)setTags:(NSArray *)tags
{
    _tags=tags;
    
    float x = 15, y = 50;
    float width = APPFRAME.size.width/3.0-30, height = 30, intervalX = 30, intervalY = 20;
    int lineCount = 3;
    for (int i=0; i<tags.count; i++)
    {
        y = (i/lineCount)*(height+intervalY)+50;
        x = (i%lineCount)*(width+intervalX)+15;
        CGRect rect=CGRectMake(x, y, width, height);
        
        GXButton* btn=[[GXButton alloc] initWithFrame:rect];
        btn.tag=200+i;
        btn.layer.cornerRadius=3;
        btn.layer.borderWidth=0.5;
        btn.layer.borderColor=[UIColor lightGrayColor].CGColor;
        btn.titleLabel.font=[UIFont fontWithName:@"STHeitiSC-Light" size:14];
        [btn setTitle:[tags objectAtIndex:i] forState:UIControlStateNormal];
        [btn setTitle:[tags objectAtIndex:i] forState:UIControlStateSelected];
        [btn setTitleColor:kDarkGrayColor forState:UIControlStateNormal];
        [btn setTitleColor:kBlueColor forState:UIControlStateSelected];
        btn.titleLabelFrame = btn.bounds;
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        btn.imageViewFrame = CGRectMake(btn.width-16, btn.height-16, 16, 16);
        btn.relayout = YES;
        [btn setImage:[UIImage imageNamed:@"tagSelected"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
    
    self.frame=CGRectMake(0, 0, APPFRAME.size.width, y+(height+10));
    
    DOImageView* sepLine=[[DOImageView alloc]initWithFrame:CGRectMake(15,self.height-0.5, self.width-15, 0.5)];
    sepLine.imgURLStr=@"sepLine.png";
    [self addSubview:sepLine];
}

-(void)cleanSelected
{
    for (int i=0; i<self.tags.count; i++)
    {
        UIButton* btn=(UIButton*)[self viewWithTag:(200+i)];
        btn.selected=NO;
        btn.backgroundColor=[UIColor whiteColor];
        btn.layer.borderColor=[UIColor lightGrayColor].CGColor;
    }
    self.curBtn=nil;
    selected=nil;
}

-(void)btnAction:(UIButton*)sender
{
    [HDUtil hidenKeyBoard];
    if (self.isSingle)
    {
        if (self.curBtn!=sender)
        {
            self.curBtn.selected=NO;
            self.curBtn.layer.borderColor=[UIColor lightGrayColor].CGColor;
            self.curBtn.backgroundColor=[UIColor whiteColor];
            self.curBtn=sender;
        }
    }
    
    sender.selected=!sender.selected;
    if (sender.selected)
    {
        sender.layer.borderColor=kBlueColor.CGColor;
    }
    else
    {
        sender.layer.borderColor=[UIColor lightGrayColor].CGColor;
    }
}

-(NSString*)selected
{
    NSString* str=nil;
    
    for (int i=0; i<self.tags.count; i++)
    {
        UIButton* btn=(UIButton*)[self viewWithTag:(200+i)];
        if (btn.selected)
        {
            if (str==nil)
            {
                str=[NSString string];
            }
            else
            {
                str=[str stringByAppendingString:@"/"];
            }
            
            str=[str stringByAppendingString:btn.titleLabel.text];
        }
    }
    return str;
}

@end
